# coding=utf-8

from scrapy import Request, Spider
from datetime import datetime
from datetime import timezone
from datetime import timedelta
from dateutil import parser
import logging

logging.basicConfig(level=logging.NOTSET)

import time
from urllib.parse import unquote
from SingleNodeSpider.items import SinglenodespiderItem as Item


class baseSpider(Spider):
    media_id = None
    name = None
    allowed_domains = None
    urls_channel = None
    
    # 如果文章正文内图需要拼接则把要拼接的头填到这里，self.get_inner_images()中就不需要再写拼接逻辑
    inner_img_splice_str = ''
    
    def start_requests(self):
        '''
        配合urls_channel使用，如果urls_channel格式变动，这个函数也要重写
        :return:
        '''
        for url, news_channel in self.urls_channel:
            yield Request(url, dont_filter=True, callback=self.parse)
    
    def parse(self, response):
        '''
        建议重写，这里只是一个模板
        :param response:
        :return:
        '''
        item_urls = self.get_item_urls(response)
        thumbs = self.get_thumbs(response)
        
        for index, item_url in enumerate(item_urls):
            if not thumbs:
                thumb = None
            else:
                thumb = thumbs[index]
            yield Request(item_url, meta={"thumb":thumb}, callback=self.parse_art)
    
    def parse_art(self, response):
        # 公共部分
        thumb = response.meta["thumb"]
        if not thumb:
            thumb = self.get_thumb(response)

        title = self.get_title(response)
        release_time = self.date_format(self.get_release_time(response))
        content = self.get_content(response)
        inner_image = self.get_inner_images(response, content)

        art_item = Item()
        art_item["thumb"] = thumb
        art_item["item_url"] = response.url
        art_item["title"] = title
        art_item["release_time"] = release_time
        art_item["content"] = content
        art_item["inner_image"] = inner_image

        yield art_item



    def get_item_urls(self, response):
        return []
    
    def get_thumbs(self, response):
        '''
        get thumbs urls. 在parse跑, 取的是整个文章列表的所有缩略图组成一个List，配合itemurls使用
        图文只要一张缩略图，要截取。
        :param response:
        :return: [thumb_url]
        '''
        return []
    
    def get_thumb(self, response):
        '''
        在parse_art使用，如果传来的meta没有thumb这在这里进行获取缩略图
        图文只要一张缩略图，要截取。
        :param response:
        :return: thumb_url
        '''
        return ''
    
    def get_inner_images(self, response, content=''):
        '''
        在parse_art使用。获取文章内图片
        :param response:
        :return: image_url
        '''
        return []
    
    def get_title(self, response):
        return ''
    
    def get_author(self, response):
        return ''
    
    def get_release_time(self, response):
        """
        :param response:
        :return: date_string
        :return: zone  [可选，默认False，返回True时进行时区转换]
        :return: delta [可选，默认0，zone为True时delta为时区数字，东八区为-8。东七区为-7]
        """
        return ''
    
    def get_content(self, response):
        return ''

    
    def date_format(self, date_string):
        if not type(date_string) == str:
            date_string, zone, delta = date_string
        else:
            zone, delta = False, 0
        ctime = parser.parse(date_string)
        if zone:
            strftime = ctime.astimezone(timezone(timedelta(hours=delta))).strftime("%Y-%m-%d %H:%M:%S")
        else:
            strftime = ctime.strftime("%Y-%m-%d %H:%M:%S")
        return strftime
